﻿using System;
using System.Collections;
using System.Collections.Generic;
using RMS.Utils.Dicom.DataStructure;
using RMS.Utils.Dicom.Commen;
using System.Xml.Linq;
using System.Xml;
namespace RMS.Utils.Dicom
{
    public class DicomFile : DicomBase
    {
        #region [Properties]
        public override bool IsDicoomFile
        {
            get { return StreamFile.IsDicomFile(); }
        }
        #endregion

        # region [Constructor]
        public DicomFile(String filePath) : base(filePath) { }
        public DicomFile() : base() { }
        #endregion

        #region [Methods]
        public override void SaveImage(string FilePath)
        {
            base.imgData.SaveAs(FilePath);
        }
        public override void SaveDicomAsXml(string path)
        {
            XmlDocument doc = new XmlDocument();
            XmlNode declaration = doc.CreateNode(XmlNodeType.XmlDeclaration, null, null);
            doc.AppendChild(declaration);
            XmlElement root = doc.CreateElement("DicomTags");
            doc.AppendChild(root);
            foreach (DataElement element in base.DicomData)
            {
                XmlElement Discription = doc.CreateElement("Discription");
                Discription.InnerText = element.tag.TagDiscription;

                XmlElement Value = doc.CreateElement("Value");
                Value.InnerText = element.value.ValueFiled.ToString();

                XmlAttribute tagName = doc.CreateAttribute("TagName");
                tagName.Value = element.tag.TagName;

                XmlElement Parent = doc.CreateElement("Tag");
                Parent.AppendChild(Discription);
                Parent.AppendChild(Value);
                Parent.Attributes.Append(tagName);
                root.AppendChild(Parent);
            }
            doc.Save(path);

        }
        #endregion

        #region [HelperMethods]
        protected override void paresDicom()
        {
            if (IsDicoomFile)
            {
                while (StreamFile.stream.Position < StreamFile.stream.Length)
                {
                    DataElement dataElement = new DataElement();
                    this.CheckDataElement(dataElement);
                    if (!StreamFile.IsPixelData)
                        this._dicomData.Add(dataElement);
                }
                StreamFile.stream.Close();
                base.imgData.ReadPixel();
            }
            else
            {
                throw new Exception("This is not a Dicom file or Not is V3.0");
            }
        }
        private void CheckDataElement(DataElement dataElement)
        {
            switch (dataElement.tag.TagName)
            {
                case Constants.CharacterRepertoireTag:
                    CharacterRepertoire.characterRepertoire(dataElement.value.ValueFiled.ToString());
                    break;
                case Constants.UIDTag:
                    TransferSyntax.CheckUID(dataElement.value.ValueFiled.ToString());
                    break;
                case Constants.SequenceDelimitation:
                case Constants.ItemDelimitation:
                    StreamFile.IsPixelData = false;
                    break;
                case Constants.SamplesPerPixelTag:
                    base.imgData.SamplesPerPixel = dataElement.value.ValueFiled.ToInt32();
                    break;
                case Constants.RowsTag:
                    base.imgData.ImgHeight = dataElement.value.ValueFiled.ToInt32();
                    break;
                case Constants.ColumnsTag:
                    base.imgData.ImgWidth = dataElement.value.ValueFiled.ToInt32();
                    break;
                case Constants.BitsAllocatedTag:
                    base.imgData.BitsAllocated = dataElement.value.ValueFiled.ToInt32();
                    break;
                case Constants.BitsStoredTag:
                    base.imgData.BitsStoredTag = dataElement.value.ValueFiled.ToInt32();
                    break;
                case Constants.HighBitTag:
                    base.imgData.HighBitTag = dataElement.value.ValueFiled.ToInt32();
                    break;
                case Constants.WindowCenter:
                    base.imgData.WindowsCentre = dataElement.value.ValueFiled.ToInt32();
                    break;
                case Constants.WindowWidth:
                    base.imgData.WindowsWidth = dataElement.value.ValueFiled.ToInt32();
                    break;
                case Constants.PixelRepresentation:
                    base.imgData.pixelRepresentation = dataElement.value.ValueFiled.ToInt32();
                    break;
            }
        }
        #endregion
    }
}